package com.eh.erd.core.controller.admin;


import com.eh.common.exception.Assert;
import com.eh.common.exception.BusinessException;
import com.eh.common.result.R;
import com.eh.common.result.ResponseEnum;
import com.eh.erd.core.pojo.entity.IntegralGrade;
import com.eh.erd.core.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 积分等级表 前端控制器
 * </p>
 *
 * @author zhuyuanwei
 * @since 2021-04-29
 */
@Api(tags = "积分等级管理")
@RestController
@RequestMapping("admin/core/integralGrade")
@Slf4j
public class AdminIntegralGradeController {

    @Resource
    private IntegralGradeService integralGradeService;

    @ApiOperation("积分等级列表")
    @GetMapping("/list")
    public R getIntegralGradeListAll(){
        log.info("info");
        log.error("error");
        log.warn("warn");

        return R.ok().data("list",integralGradeService.list());
    }

    @ApiOperation(value ="根据id删除积分等级记录",notes = "逻辑删除")
    @DeleteMapping("/remove/{id}")
    public R  removeById(@ApiParam(value = "数据id",example = "100",required = true) @PathVariable Long id){
        boolean b = integralGradeService.removeById(id);
        if(b){
            return R.ok().message("删除成功");
        }
            return R.error().message("删除失败");
    }

    @ApiOperation("新增积分等级")
    @PutMapping("/save")
    public R save(@ApiParam(value = "积分等级对象",required = true) @RequestBody IntegralGrade integralGrade){
        boolean save = integralGradeService.save(integralGrade);

        Assert.notNull(integralGrade.getBorrowAmount(),ResponseEnum.BORROW_AMOUNT_NULL_ERROR);

        if(save){
            return R.ok().message("保存成功");
        }
        return R.error().message("保存失败");
    }

    @ApiOperation("根据id获取积分等级")
    @GetMapping("/get/{id}")
    public R getById(@ApiParam(value = "积分等级id",required = true,example = "1") @PathVariable Long id){
        IntegralGrade byId = integralGradeService.getById(id);
        if(byId!=null){
            return R.ok().data("record",byId);
        }
        return R.error().message("获取失败");
    }

    @ApiOperation("更新积分等级")
    @PostMapping("/update")
    public R updateById(@ApiParam(value = "积分等级id",required = true) @RequestBody IntegralGrade integralGrade){
        boolean update = integralGradeService.updateById(integralGrade);
        if(update){
            return R.ok().message("更新成功");
        }
        return R.error().message("更新失败");
    }



}

